#include "stdio.h"
#include "fcntl.h"
#include "string.h"
#include "unistd.h"
#include "stdlib.h"

int main(int argc, char const *argv[])
{
    // 打开文件
    int fd1 = open("01_file_write.c", O_RDONLY);
    if (fd1 == -1)
    {
        perror("01_file_write 打开失败");
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("01_file_write 打开成功 \n");
    }

    int fd2 = open("a.txt", O_RDWR | O_CREAT, 0666);
    if (fd2 == -1)
    {
        perror("a.txt 打开失败");
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("a.txt 打开成功 %d\n", fd2);
    }

    // 复制
    char buff[100];
    ssize_t len = 0;
    while ((len = read(fd1, buff, sizeof(buff))) > 0)
    {
        write(fd2, buff, len);
        write(STDOUT_FILENO, buff, len);
    }

    close(fd1);
    close(fd2);

    return 0;
}
